<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of extendedcontrol
 *
 * @author Chris Sim
 */

 Yii::import('zii.widgets.jui.CJuiInputWidget');


 
class extendedcontrol extends CJuiInputWidget {
//put your code here

    public $controlType = 'textfield';

    public function run() {
    //make control
    //
        list($name,$id)=$this->resolveNameID();

        if(isset($this->htmlOptions['id']))
            $id=$this->htmlOptions['id'];
        else
            $this->htmlOptions['id']=$id;
        if(isset($this->htmlOptions['name']))
            $name=$this->htmlOptions['name'];
        else
            $this->htmlOptions['name']=$name;
        if($this->hasModel()) {
            switch($this->controlType) {
                case 'textfield': echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
                    break;
            }
        }
        $options=CJavaScript::encode($this->options);

        $js = "jQuery('#{$id}').extendedcontrols($options);";

        if(Yii::app()->request->isAjaxRequest)
            echo CHtml::script($js);
        else
            $this->publishAssets($id, $js);
    }

    private function publishAssets($id,$js='') {
        $baseUrl = Yii::getPathOfAlias('application.widgets.extendedcontrol.script');
        $publishurl = Yii::app()->getAssetManager()->publish($baseUrl);
        $cs = Yii::app()->getClientScript();

        $cs->registerCoreScript('jquery');
        $cs->registerCssFile($publishurl.'/extendedcontrols.css');
        $cs->registerScriptFile($publishurl.'/extendedcontrols.js');

        if(!empty($js)) {
            $cs->registerScript(__CLASS__.'#'.$id, $js, CClientScript::POS_READY);
        }
    }
}
?>
